package com.gec.hrm.dao.impl;

import com.gec.hrm.pojo.Dept;
import com.gec.hrm.pojo.Job;
import com.gec.hrm.pojo.User;
import com.gec.hrm.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DeptDaoImpl extends JDBCUtils<Dept> implements DeptDao{

    @Override
    public List<Dept> getAllDept() {
        return query("select * from dept_inf");
    }
    @Override
    public Dept getBean(ResultSet rs) {
        Dept dept=new Dept();
        try {
            dept.setId(rs.getInt("ID"));
            dept.setName(rs.getString("NAME"));
            dept.setRemark(rs.getString("REMARK"));
            dept.setStatus(rs.getInt("state"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return dept;
    }

    @Override
    public List<Dept> deptList(String name, int page, int limit) {
        String sql = "select * from dept_inf where 1=1 ";//万能条件
        //需要对条件做一下判断，不为空
        //再输入某个条件后，另外一个条件不是null,而是一个空的字符串
        if(name !=null && !"".equals(name))
        {
            sql+= "and name like '%"+name+"%'";
        }
        sql+=" limit "+(page-1)*limit +","+limit+"";
        List<Dept> depts = query(sql);
        return depts;
    }

    @Override
    public boolean checkDeptName(String name) {
        List<Dept> depts= query("select * from dept_inf where name = ?",name);
        if(depts.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public int addDept(Dept dept) {
        return update("insert into dept_inf  values(null,?,?,null)",
                dept.getName(), dept.getRemark());
    }

    @Override
    public int updDept(Dept dept) {
        return update("update dept_inf set name=?,remark=? where id=?",
                dept.getName(),dept.getRemark(),dept.getId());

    }

    @Override
    public int count() {
        List<Dept> depts= query("select * from dept_inf");
        return depts.size();
    }

    @Override
    public int delDept(int id) {
        List<Dept> depts= query("select dept_inf.* from dept_inf,employee_inf where dept_inf.id=? and dept_inf.id=employee_inf.dept_id",id);
        if(depts.size()>0){
            return -1;
        }
        return update("delete from dept_inf where id=?",id);
    }

}
